package com.youwu.demo02.FileUpload;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class TCPClient {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("c:\\1.jpg");
        Socket socket = new Socket("127.0.0.1", 8888);
        OutputStream os = socket.getOutputStream();
        int len = 0;
        byte[] bytes = new byte[1024];
        // fis.read(bytes)读取本地文件，结束标记是读取-1结束
        // while不会读取到-1，也就不会把标记写给服务器
        while(-1 != (len = fis.read(bytes))) {
            os.write(bytes, 0, len);
        }

        // 解决方法
        socket.shutdownOutput();

        InputStream is = socket.getInputStream();
        while(-1 != (len = is.read(bytes))) {
            System.out.println(new String(bytes, 0, len));
        }

        fis.close();
        socket.close();
    }
}
